# 创建应用程序图标
# 这个脚本会基于 XAML 中定义的图标生成一个 .ico 文件

Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName PresentationCore, PresentationFramework, WindowsBase

$iconPath = Join-Path $PSScriptRoot "Resources\app.ico"

# 创建一个简单的图标（蓝色背景 + 白色文档 + 红色M标记）
function Create-Icon {
    param([int]$size)
    
    $bitmap = New-Object System.Drawing.Bitmap($size, $size)
    $graphics = [System.Drawing.Graphics]::FromImage($bitmap)
    $graphics.SmoothingMode = [System.Drawing.Drawing2D.SmoothingMode]::AntiAlias
    
    # 填充蓝色圆角矩形背景
    $blueBrush = New-Object System.Drawing.SolidBrush([System.Drawing.Color]::FromArgb(255, 33, 150, 243))
    $path = New-Object System.Drawing.Drawing2D.GraphicsPath
    $radius = [int]($size * 0.125) # 4/32 比例
    $rect = New-Object System.Drawing.Rectangle(0, 0, $size, $size)
    
    # 创建圆角矩形
    $path.AddArc($rect.Left, $rect.Top, $radius * 2, $radius * 2, 180, 90)
    $path.AddArc($rect.Right - $radius * 2, $rect.Top, $radius * 2, $radius * 2, 270, 90)
    $path.AddArc($rect.Right - $radius * 2, $rect.Bottom - $radius * 2, $radius * 2, $radius * 2, 0, 90)
    $path.AddArc($rect.Left, $rect.Bottom - $radius * 2, $radius * 2, $radius * 2, 90, 90)
    $path.CloseFigure()
    
    $graphics.FillPath($blueBrush, $path)
    
    # 绘制白色文档图标
    $whiteBrush = New-Object System.Drawing.SolidBrush([System.Drawing.Color]::White)
    $whitePen = New-Object System.Drawing.Pen($whiteBrush, [Math]::Max(1, $size / 32))
    
    $docPath = New-Object System.Drawing.Drawing2D.GraphicsPath
    $scale = $size / 32.0
    
    # 文档主体 (scaled from: M8,6 L20,6 L24,10 L24,26 L8,26 Z)
    $docPath.AddLine([int](8 * $scale), [int](6 * $scale), [int](20 * $scale), [int](6 * $scale))
    $docPath.AddLine([int](20 * $scale), [int](6 * $scale), [int](24 * $scale), [int](10 * $scale))
    $docPath.AddLine([int](24 * $scale), [int](10 * $scale), [int](24 * $scale), [int](26 * $scale))
    $docPath.AddLine([int](24 * $scale), [int](26 * $scale), [int](8 * $scale), [int](26 * $scale))
    $docPath.CloseFigure()
    
    # 折角 (M20,6 L20,10 L24,10)
    $docPath.StartFigure()
    $docPath.AddLine([int](20 * $scale), [int](6 * $scale), [int](20 * $scale), [int](10 * $scale))
    $docPath.AddLine([int](20 * $scale), [int](10 * $scale), [int](24 * $scale), [int](10 * $scale))
    
    $graphics.FillPath($whiteBrush, $docPath)
    
    # 绘制红色 M 标记
    $redBrush = New-Object System.Drawing.SolidBrush([System.Drawing.Color]::FromArgb(255, 255, 107, 107))
    
    $mPath = New-Object System.Drawing.Drawing2D.GraphicsPath
    # M12,14 L12,22 L13.5,22 L13.5,16 L16,19 L18.5,16 L18.5,22 L20,22 L20,14 L18.5,14 L16,17.5 L13.5,14 Z
    $points = @(
        [System.Drawing.PointF]::new(12 * $scale, 14 * $scale),
        [System.Drawing.PointF]::new(12 * $scale, 22 * $scale),
        [System.Drawing.PointF]::new(13.5 * $scale, 22 * $scale),
        [System.Drawing.PointF]::new(13.5 * $scale, 16 * $scale),
        [System.Drawing.PointF]::new(16 * $scale, 19 * $scale),
        [System.Drawing.PointF]::new(18.5 * $scale, 16 * $scale),
        [System.Drawing.PointF]::new(18.5 * $scale, 22 * $scale),
        [System.Drawing.PointF]::new(20 * $scale, 22 * $scale),
        [System.Drawing.PointF]::new(20 * $scale, 14 * $scale),
        [System.Drawing.PointF]::new(18.5 * $scale, 14 * $scale),
        [System.Drawing.PointF]::new(16 * $scale, 17.5 * $scale),
        [System.Drawing.PointF]::new(13.5 * $scale, 14 * $scale)
    )
    $mPath.AddPolygon($points)
    $graphics.FillPath($redBrush, $mPath)
    
    $graphics.Dispose()
    $path.Dispose()
    $docPath.Dispose()
    $mPath.Dispose()
    $blueBrush.Dispose()
    $whiteBrush.Dispose()
    $whitePen.Dispose()
    $redBrush.Dispose()
    
    return $bitmap
}

Write-Host "正在创建应用程序图标..." -ForegroundColor Cyan

# 创建多个尺寸的图标
$sizes = @(16, 32, 48, 256)
$icon = $null

try {
    # 创建临时文件来存储不同尺寸的图标
    $tempFiles = @()
    
    foreach ($size in $sizes) {
        Write-Host "  生成 ${size}x${size} 图标..." -ForegroundColor Gray
        $bitmap = Create-Icon -size $size
        $tempFile = [System.IO.Path]::GetTempFileName() + ".png"
        $bitmap.Save($tempFile, [System.Drawing.Imaging.ImageFormat]::Png)
        $tempFiles += $tempFile
        $bitmap.Dispose()
    }
    
    # 使用最大的图标作为基础
    $mainBitmap = Create-Icon -size 256
    $iconHandle = $mainBitmap.GetHicon()
    $icon = [System.Drawing.Icon]::FromHandle($iconHandle)
    
    # 保存图标
    $stream = [System.IO.File]::Create($iconPath)
    $icon.Save($stream)
    $stream.Close()
    
    # 清理
    foreach ($tempFile in $tempFiles) {
        if (Test-Path $tempFile) {
            Remove-Item $tempFile -Force
        }
    }
    
    $mainBitmap.Dispose()
    $icon.Dispose()
    
    Write-Host "✓ 图标创建成功: $iconPath" -ForegroundColor Green
    Write-Host ""
    Write-Host "提示: 图标已保存到 Resources\app.ico" -ForegroundColor Yellow
    Write-Host "      项目会自动使用此图标" -ForegroundColor Yellow
}
catch {
    Write-Host "✗ 创建图标失败: $($_.Exception.Message)" -ForegroundColor Red
    exit 1
}

